﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace THenu.TinyTools.ZDemonstrate
{
    public partial class FrmGraphicRotate : Form
    {
        public FrmGraphicRotate()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmGraphicRotate_MouseDown(object sender, MouseEventArgs e)
        {


            Graphics graFrame = this.CreateGraphics();
            graFrame.Clear(this.BackColor);
            Point axesCenter = new Point(this.Width / 2, this.Height / 2);
            Rectangle axesPoint = new Rectangle(new Point(axesCenter.X - 2, axesCenter.Y - 2), new Size(4, 4));
            graFrame.DrawEllipse(Pens.Red, axesPoint);
            graFrame.FillEllipse(Brushes.Red, axesPoint);
            graFrame.DrawLine(Pens.Blue, axesCenter.X - 200, axesCenter.Y, axesCenter.X + 200, axesCenter.Y);
            graFrame.DrawLine(Pens.Blue, axesCenter.X, axesCenter.Y - 200, axesCenter.X, axesCenter.Y + 200);
            Point mousePoint = new Point(e.X, e.Y);

            Rectangle mousePos = new Rectangle(new Point(mousePoint.X - 4, mousePoint.Y - 4), new Size(8, 8));
            graFrame.FillEllipse(Brushes.BlueViolet, mousePos);


            graFrame.DrawLine(Pens.GreenYellow, axesCenter.X, axesCenter.Y, mousePoint.X, mousePoint.Y);


            Point mathPoint = new Point(mousePoint.X - axesCenter.X, axesCenter.Y - mousePoint.Y);
            double angle = GetRoate(new Point(0, 0), mathPoint);

            string msg = string.Format("Mouse : {0:D},{1:D}  ,Angle : {2:F3} ", mathPoint.X, mathPoint.Y, angle);
            graFrame.DrawString(msg, SystemFonts.DefaultFont, Brushes.Black, new PointF(mousePoint.X + 10, mousePoint.Y + 10));
        }

        /// <summary>
        /// 计算出两个点的仰角【以起始点为圆心建立数学坐标系】
        /// </summary>
        /// <returns></returns>
        public static double GetRoate(Point StartPoint, Point EndPoint)
        {
            if (StartPoint.Equals(EndPoint))
                return 0.0D;
            double AB = (double)(EndPoint.X - StartPoint.X);
            double BC = (double)(EndPoint.Y - StartPoint.Y);
            double cosA = AB / Math.Sqrt(Convert.ToDouble(AB * AB + BC * BC));
            double jdA = Math.Acos(cosA) * 180 / Math.PI;
            if (BC < 0)
                return (double)(360 - jdA);

            return (double)jdA;
        }
    }
}
